using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using SqlSugar;
using ProductService.Domain.Models;
using ProductService.Domain.Repositories;
using ProductService.Infrastructure.Repositories;
using SharedKernel.Infrastructure.Repositories;

namespace ProductService.Infrastructure.Repositories
{
    /// <summary>
    /// 商品仓储实现
    /// </summary>
    public class ProductRepository : SqlSugarRepository<Product, Guid>, IProductRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serviceProvider">服务提供器</param>
        /// <param name="context">SqlSugar客户端</param>
        public ProductRepository(IServiceProvider serviceProvider, ISqlSugarClient context) : base(serviceProvider, context)
        { }

        /// <summary>
        /// 根据名称获取商品
        /// </summary>
        /// <param name="name">商品名称</param>
        /// <returns>商品实体</returns>
        public async Task<Product> GetByNameAsync(string name)
        {
            return await Entities.FirstAsync(p => p.Name == name);
        }

        /// <summary>
        /// 根据分类获取商品列表
        /// </summary>
        /// <param name="category">商品分类</param>
        /// <returns>商品列表</returns>
        public async Task<List<Product>> GetByCategoryAsync(string category)
        {
            return await Entities.Where(p => p.Category == category).ToListAsync();
        }

        /// <summary>
        /// 搜索商品
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <returns>商品列表</returns>
        public async Task<List<Product>> SearchAsync(string keyword)
        {
            return await Entities
                .Where(p => p.Name.Contains(keyword) || p.Description.Contains(keyword))
                .ToListAsync();
        }
    }
}